package com.lcx.plugin.ui;

import cn.hutool.core.util.StrUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.lcx.plugin.dict.GlobalDict;
import com.lcx.plugin.dto.DbConfigDTO;
import com.lcx.plugin.dto.MySettingsStorageDTO;
import com.lcx.plugin.enums.DatabaseType;
import com.lcx.plugin.handle.JdbcHandle;
import com.lcx.plugin.service.MySettingsStorageService;
import com.lcx.plugin.tool.CacheDataUtils;
import com.lcx.plugin.tool.ComponentUtil;
import com.lcx.plugin.tool.PassUtil;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.Objects;

/**
 * @author daxiong
 * @date 2023/6/16 下午2:54
 */
public class DbSettings extends DialogWrapper {

    private JLabel urlLabel;
    private JTextField urlField;
    private JLabel userLable;
    private JTextField userField;
    private JLabel driverLabel;
    private JTextField driverField;
    private JLabel pwdLabel;
    private JButton testBtn;
    private JCheckBox rememberCheckBox;
    private JPasswordField pwdField;

    /**
     * 主面板
     */
    private JPanel contentPane;
    /**
     * 数据缓存工具类
     */
    private final CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();
    /**
     * 项目对象
     */
    private final Project project;

    /**
     * 构造方法
     */
    public DbSettings(Project project) {
        super(project);
        this.project = project;
        // 初始化界面
        this.init();
        // 初始化事件
        this.initEvent();
        setTitle(GlobalDict.TITLE_INFO);
    }

    @Override
    public void init() {
        super.init();
        DbConfigDTO dbConfig = cacheDataUtils.getDbConfig();
        this.urlField.setText(dbConfig.getUrl());
        this.driverField.setText(dbConfig.getDriver());
        this.userField.setText(dbConfig.getUser());
        MySettingsStorageDTO settingsStorage = MySettingsStorageService.getSettingsStorage();
        this.rememberCheckBox.setSelected(settingsStorage.isRememberPwd());

        String pass = PassUtil.getPass(dbConfig.getUrl(), dbConfig.getUser());
        this.pwdField.setText(pass);
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        return this.contentPane;
    }


    private void initEvent() {
        DbConfigDTO dbConfig = cacheDataUtils.getDbConfig();
        // 测试连接按钮
        testBtn.addActionListener(e -> {
            String urlFieldText = this.urlField.getText();
            if (StrUtil.isBlank(urlFieldText)) {
                Messages.showWarningDialog("jdbcUrl不能为空！！", GlobalDict.TITLE_INFO);
                return;
            }
            dbConfig.setUrl(urlFieldText);
            String driverFieldText = this.driverField.getText();
            if (StrUtil.isBlank(driverFieldText)) {
                Messages.showWarningDialog("driverClass不能为空！！", GlobalDict.TITLE_INFO);
                return;
            }
            dbConfig.setDriver(driverFieldText);
            String userFieldText = this.userField.getText();
            if (StrUtil.isBlank(userFieldText)) {
                Messages.showWarningDialog("user不能为空！！", GlobalDict.TITLE_INFO);
                return;
            }
            dbConfig.setUser(userFieldText);
            String pwdFieldText = this.pwdField.getText();
            if (StrUtil.isBlank(pwdFieldText)) {
                Messages.showWarningDialog("password不能为空！！", GlobalDict.TITLE_INFO);
                return;
            }
            dbConfig.setPass(pwdFieldText);

            cacheDataUtils.setDbConfig(dbConfig);
            // 测试连接
            testConn(dbConfig);
        });


    }

    /**
     * 检测连接是否成功
     *
     * @param dbConfig
     * @return void
     * @author daxiong
     * @date 2023/6/20 下午2:34
     */
    private boolean testConn(DbConfigDTO dbConfig) {
        JdbcHandle jdbcHandle = new JdbcHandle(dbConfig);
        boolean testConnect = jdbcHandle.testConnect();
        if (testConnect) {
            ComponentUtil.showNotification(project, "测试连接成功", true);
        } else {
            ComponentUtil.showNotification(project, "测试连接失败", false);
        }
        return testConnect;
    }

    @Override
    protected void doOKAction() {
        // 校验密码不能为空
        String pwdFieldText = this.pwdField.getText();
        if (StrUtil.isBlank(pwdFieldText)) {
            Messages.showWarningDialog("password不能为空！！", GlobalDict.TITLE_INFO);
            return;
        }
        boolean ok = onOk();
        if (!ok) {
            return;
        }
        // OK的默认行为最后执行
        super.doOKAction();
        // 下一个界面
        new SelectFile(project).show();
    }

    private boolean onOk() {
        // 设置密码
        DbConfigDTO dbConfig = cacheDataUtils.getDbConfig();
        dbConfig.setPass(this.pwdField.getText());
        cacheDataUtils.setDbConfig(dbConfig);
        if (this.rememberCheckBox.isSelected()) {
            // 记住密码
            PassUtil.savePass(dbConfig.getUrl(), dbConfig.getUser(), dbConfig.getPass());
        } else {
            PassUtil.savePass(dbConfig.getUrl(), dbConfig.getUser(), null);
        }

        // 测试连接
        return testConn(dbConfig);
    }

}
